// 导入react
import React from 'react';
// 导入路由组件
import { Switch,Route,Redirect } from 'react-router-dom';
// 导入组件
import Login from '@/pages/Login';
import Main from '@/pages/Main';
import ProductList from '@/pages/product/ProductList';
import ProductAdd from '@/pages/product/ProductAdd';
import Bar from '@/pages/chart/Bar';
import Line from '@/pages/chart/Line';
import Pie from '@/pages/chart/Pie';
import User from '@/pages/user/User';





const  routes=[
    {
        path:'/',
        exact:true,
        to:'/admin/product-list'
    },
    {
        path:'/admin',
        exact:false,
        component:Main,
        routes:[
            {
                path:'/admin/product-list',
                component:ProductList,
                exact:false
            },
            {
                path:'/admin/product-add',
                component:ProductAdd,
                exact:false
            },
            {
                path:'/admin/chart-bar',
                component:Bar,
                exact:false
            },
            {
                path:'/admin/chart-line',
                component:Line,
                exact:false
            },
            {
                path:'/admin/chart-pie',
                component:Pie,
                exact:false
            },
            {
                path:'/admin/user-pwd',
                component:User,
                exact:false
            }
        ]
    },
    {
        path:'/login',
        component:Login,
        exact:false
    },
    {
        path:'**',
        exact:false,
        to:'/login'
    }
];

function router(){
    return <Switch>
                {routes.map((item,index)=>(
                <Route key={index}  exact={item.exact} path={item.path} render={(routeProps)=>{
                    if(item.component){
                        // 如果没有登录, 重定向到登录页面
                        if(!sessionStorage.getItem('token')&&item.path!='/login'){
                            return <Redirect to='/login'/>
                        }
                        return <item.component {...routeProps} routes={item.routes}/>
                    }else{
                        return <Redirect to={item.to}/>
                    }
                }} />
            ))}
        </Switch>
}
export default router;